package com.aw.guiguzi.widgets

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.aw.baselib.base.AwBaseConstant
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwPopupwindowUtil
import com.aw.baselib.util.AwRecyclerViewUtil
import com.aw.baselib.util.AwToastUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.adapter.NumAdapter
import com.aw.guiguzi.base.MyApp
import com.aw.guiguzi.bean.NumItemBean
import com.aw.guiguzi.enums.NumBettingEnums
import com.aw.guiguzi.util.AppCacheUtil
import com.chad.library.adapter.base.BaseQuickAdapter

class NumPopupWindow(
    private val mActivity: Activity,
    callback: OnResultCallback?,
    dismissListener: OnDismissListener?
) : PopupWindow() {
    private val contentView: View
    private val mAdapter: NumAdapter
    private var mList: MutableList<NumItemBean> = ArrayList()

    init {
        mList = MyApp.mAllBettingNumsList.toMutableList()
        contentView = View.inflate(mActivity, R.layout.view_popupwindow_num, null)
        val rcv_data = contentView.findViewById<View>(R.id.rcv_data) as RecyclerView
        val tv_cancel = contentView.findViewById<TextView>(com.aw.baselib.R.id.tv_cancel)
        val tv_confirm = contentView.findViewById<TextView>(com.aw.baselib.R.id.tv_confirm)
        mAdapter = NumAdapter()
        AwRecyclerViewUtil.setRecyclerViewLinearlayout(mActivity, rcv_data, mAdapter, false)
        mAdapter.addAllData(mList)
        val rg_config = contentView.findViewById<RadioGroup>(R.id.rg_config)
        setDefaultCheckedConfig(rg_config)
        tv_cancel.setOnClickListener { v: View? -> dismiss() }
        tv_confirm.setOnClickListener { v: View? ->
            if (null != callback) {
                val selectList: MutableList<NumItemBean?> = ArrayList()
                for (itemBean in mList) {
                    if (itemBean!!.isChecked) {
                        selectList.add(itemBean)
                    }
                }
                if (AwDataUtil.isEmpty(selectList)) {
                    AwToastUtil.showShort(mActivity, "请至少选择一项数据")
                    return@setOnClickListener
                }
                callback.onResult(selectList, "")
            }
            dismiss()
        }
        rg_config.setOnCheckedChangeListener { group: RadioGroup?, checkedId: Int ->
            setListItemCount(
                checkedId
            )
        }
        contentView.findViewById<View>(R.id.iv_delConfigNums).setOnClickListener { v: View? ->
            for (itemBean in mList) {
                itemBean!!.isChecked = false
            }
            mAdapter.notifyDataSetChanged()
        }
        mAdapter.setOnItemChildClickListener { adapter: BaseQuickAdapter<*, *>, view: View, position: Int ->
            if (view.id == R.id.cBox_status) {
                val itemBean = adapter.getItem(position) as NumItemBean?
                itemBean!!.isChecked = !itemBean.isChecked
                mAdapter.notifyDataSetChanged()
            }
        }
        setContentView(contentView)
        this.width = ViewGroup.LayoutParams.MATCH_PARENT
        this.height = ViewGroup.LayoutParams.WRAP_CONTENT
        this.animationStyle = com.aw.baselib.R.style.sytle_anim_pop_in_out
        this.isFocusable = true
        this.isTouchable = true
        this.isOutsideTouchable = true
        setBackgroundDrawable(BitmapDrawable())
        this.update()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.overlapAnchor = true
        }
        if (dismissListener == null) {
            setOnDismissListener {
                val lp = mActivity.window.attributes
                lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_NORMAL
                mActivity.window.attributes = lp
            }
        } else {
            setOnDismissListener(dismissListener)
        }
    }

    private fun setDefaultCheckedConfig(rg_config: RadioGroup) {
        var index = 0
        if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_1_1_10.name) {
            index = 0
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_1_1_20.name) {
            index = 1
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_1_1_40.name) {
            index = 2
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_2_2_20.name) {
            index = 3
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_2_2_40.name) {
            index = 4
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_3_3_30.name) {
            index = 5
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_4_4_40.name) {
            index = 6
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_5_5_50.name) {
            index = 7
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_5_5_100.name) {
            index = 8
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_10_10_100.name) {
            index = 9
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_10_10_200.name) {
            index = 10
        } else if (AppCacheUtil.getBettingConfigCount() == NumBettingEnums.MODE_CUSTOM.name) {
            index = 11
        }
        val radioButton = rg_config.getChildAt(index) as RadioButton
        radioButton.isChecked = true
    }

    private fun setListItemCount(id: Int) {
        if (AwDataUtil.isEmpty(mList)) {
            return
        }
        for (itemBean in mList) {
            when (id) {
                R.id.rBn1_1_10 -> {
                    itemBean!!.setSilverCount(1)
                    itemBean.setSkinCount(1)
                    itemBean.setGemstoneCount(10)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_1_1_10.name)
                }

                R.id.rBn1_1_20 -> {
                    itemBean!!.setSilverCount(1)
                    itemBean.setSkinCount(1)
                    itemBean.setGemstoneCount(20)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_1_1_20.name)
                }

                R.id.rBn1_1_40 -> {
                    itemBean!!.setSilverCount(1)
                    itemBean.setSkinCount(1)
                    itemBean.setGemstoneCount(40)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_1_1_40.name)
                }

                R.id.rBn2_2_20 -> {
                    itemBean!!.setSilverCount(2)
                    itemBean.setSkinCount(2)
                    itemBean.setGemstoneCount(20)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_2_2_20.name)
                }

                R.id.rBn2_2_40 -> {
                    itemBean!!.setSilverCount(2)
                    itemBean.setSkinCount(2)
                    itemBean.setGemstoneCount(40)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_2_2_40.name)
                }

                R.id.rBn3_3_30 -> {
                    itemBean!!.setSilverCount(3)
                    itemBean.setSkinCount(3)
                    itemBean.setGemstoneCount(30)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_3_3_30.name)
                }

                R.id.rBn4_4_40 -> {
                    itemBean!!.setSilverCount(4)
                    itemBean.setSkinCount(4)
                    itemBean.setGemstoneCount(40)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_4_4_40.name)
                }

                R.id.rBn5_5_50 -> {
                    itemBean!!.setSilverCount(5)
                    itemBean.setSkinCount(5)
                    itemBean.setGemstoneCount(50)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_5_5_50.name)
                }

                R.id.rBn5_5_100 -> {
                    itemBean!!.setSilverCount(5)
                    itemBean.setSkinCount(5)
                    itemBean.setGemstoneCount(100)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_5_5_100.name)
                }

                R.id.rBn10_10_100 -> {
                    itemBean!!.setSilverCount(10)
                    itemBean.setSkinCount(10)
                    itemBean.setGemstoneCount(200)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_10_10_100.name)
                }

                R.id.rBn10_10_200 -> {
                    itemBean!!.setSilverCount(10)
                    itemBean.setSkinCount(10)
                    itemBean.setGemstoneCount(200)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_10_10_200.name)
                }

                R.id.rBtn_custom, -1 -> {
                    itemBean!!.setSilverCount(0)
                    itemBean.setSkinCount(0)
                    itemBean.setGemstoneCount(0)
                    AppCacheUtil.setBettingConfigCount(NumBettingEnums.MODE_CUSTOM.name)
                }
            }
        }
        mAdapter.notifyDataSetChanged()
    }

    /**
     * 展示窗口
     */
    fun show(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        this.showAsDropDown(parent, 0, 0)
    }

    fun showAsLocation(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        val windowPos = AwPopupwindowUtil.calculatePopWindowPos(parent, contentView)
        val xOff = 20 // 可以自己调整偏移
        windowPos[0] -= xOff
        showAtLocation(parent, Gravity.TOP or Gravity.START, windowPos[0], windowPos[1])
    }

    fun showAsLocationNoAlpha(parent: View?) {
        val windowPos = AwPopupwindowUtil.calculatePopWindowPos(parent, contentView)
        val xOff = 20 // 可以自己调整偏移
        windowPos[0] -= xOff
        showAtLocation(parent, Gravity.TOP or Gravity.START, windowPos[0], windowPos[1])
    }

    fun showAsLocationBottom(parent: View?) {
        val lp = mActivity.window.attributes
        //        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK;
        mActivity.window.attributes = lp
        showAtLocation(parent, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
    }

    /**
     * calendar菜单选择监听接口
     */
    interface OnResultCallback {
        fun onResult(list: List<NumItemBean?>?, desc: String?)
    }
}